package com.mxpio.erp.inventory.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.inventory.entity.Warehouse;
import com.mxpio.erp.common.inventory.service.WarehouseService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "WarehouseController", tags = { "仓库管理接口" })
@RestController
@RequestMapping("/erp/inventory/wh/")
public class WarehouseController {
	
	@Autowired
	private WarehouseService warehouseSerivce;

	@GetMapping("list")
	@ApiOperation(value = "仓库列表", notes = "获取仓库列表", httpMethod = "GET")
	public Result<List<Warehouse>> list(String criteria) throws UnsupportedEncodingException {
		Criteria c = CriteriaUtils.json2Criteria(criteria);
		List<Warehouse> warehouses = warehouseSerivce.list(Warehouse.class, c);
		return Result.OK(warehouses);
	}
	
	@GetMapping("page")
	@ApiOperation(value = "仓库列表", notes = "获取仓库列表", httpMethod = "GET")
	public Result<Page<Warehouse>> page(String criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Criteria c = CriteriaUtils.json2Criteria(criteria);
		Page<Warehouse> page = warehouseSerivce.listPage(Warehouse.class, pageAble, c);
		return Result.OK(page);
	}
	
	@GetMapping("list/{whCode}")
	@ApiOperation(value = "根据whCode获取仓库", notes = "根据whCode获取仓库", httpMethod = "GET")
	public Result<Warehouse> getById(@PathVariable(name = "whCode", required = true) String whCode) {
		Warehouse warehouse = warehouseSerivce.getById(Warehouse.class, whCode);
		return Result.OK(warehouse);
	}
	
	@PostMapping("add")
	@ApiOperation(value = "新增仓库", notes = "新增仓库", httpMethod = "POST")
	public Result<Warehouse> add(Warehouse warehouse) {
		warehouseSerivce.save(warehouse);
		return Result.OK(warehouse);
	}
	
	@PutMapping("edit")
	@ApiOperation(value = "编辑仓库", notes = "编辑仓库（全量）", httpMethod = "PUT")
	public Result<Warehouse> edit(Warehouse warehouse) {
		warehouseSerivce.update(warehouse);
		return Result.OK(warehouse);
	}
	
	@DeleteMapping("remove/{whCodes}")
	@ApiOperation(value = "删除仓库", notes = "删除仓库", httpMethod = "DELETE")
	public Result<Warehouse> remove(@PathVariable(name = "whCodes", required = true) String whCodes) {
		String whCode[] = whCodes.split(",");
		for(String key : whCode){
			warehouseSerivce.delete(Warehouse.class, key);
		}
		return Result.OK();
	}
	
}
